package com.ad.payload.model;



import com.ad.payload.enums.ResultCode;

import java.io.Serializable;

/**
 * 消息返回类, 控制层返回视图规定需要调用, 服务层返回控制层规定需要调用
 */
public class ResultBean<T> implements Serializable {



    private String msg = "成功";
    private int code = ResultCode.SUCCESS.getCode();
    private T data;

    public ResultBean() {
        super();
    }

    public ResultBean(T data) {
        super();
        this.data = data;
    }

    public ResultBean(Throwable e) {
        super();
        this.msg = e.getMessage();
        this.code = ResultCode.FAILED.getCode();
    }

    public ResultBean(Integer status, Throwable e) {
        super();
        this.msg = e.getMessage();
        this.code = status;
    }

    public ResultBean(Integer status, String msg) {
        super();
        this.msg = msg;
        this.code = status;
    }

    /**
     * 成功返回
     *
     * @return ResultBean 返回信息类
     */
    public static ResultBean success() {
        return new ResultBean();
    }

    /**
     * 成功返回
     *
     * @param object obj实体
     * @return ResultBean 返回信息类
     */
    public static ResultBean success(Object object) {
        return new ResultBean(object);
    }


    /**
     * 失败返回
     *
     * @param status 失败状态码
     * @param msg    失败提示语句
     * @return ResultBean
     */
    public static ResultBean build(Integer status, String msg) {
        return new ResultBean(status, msg);
    }

    /**
     * 失败返回
     *
     * @param throwable 异常
     * @return ResultBean
     */
    public static ResultBean build(Throwable throwable) {
        return new ResultBean(throwable);
    }

    /**
     * 失败返回
     *
     * @param throwable 异常
     * @param status 失败状态码
     * @return ResultBean
     */
    public static ResultBean build(Integer status, Throwable throwable) {
        return new ResultBean(status, throwable);
    }

    /**
     * 失败返回
     *
     * @param status 失败状态码
     * @param object partner
     * @return ResultBean
     */
    public static ResultBean build(Integer status, Object object) {
        return new ResultBean(object);
    }


    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    @Override
    public String toString() {
        return "{" +
                "msg='" + msg + '\'' +
                ", code=" + code +
                ", data=" + data +
                '}';
    }
}